﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ViewModel;

namespace IssueTracker
{
    public partial class Login : Form
    {
        /*draggable window*/
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        /*Rounded corner */
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect, // x-coordinate of upper-left corner
            int nTopRect, // y-coordinate of upper-left corner
            int nRightRect, // x-coordinate of lower-right corner
            int nBottomRect, // y-coordinate of lower-right corner
            int nWidthEllipse, // height of ellipse
            int nHeightEllipse // width of ellipse
        );

        public StickyForm objStickyForm;
        public Login()
        {
            InitializeComponent();
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 30, 30));
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint, true);
        }

        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
        private void Login_Load(object sender, EventArgs e)
        {
            //string username = IssueTracker.Properties.Settings.Default.username;
            //string psw = IssueTracker.Properties.Settings.Default.password;
            //if ((string.Compare(username, "admin9") == 0 && string.Compare(psw, "admin") == 0)
            //    || (string.Compare(username, "obamakfc") == 0 && string.Compare(psw, "user") == 0))
            //{
            //    objStickyForm.ShowMainWindow();
            //    objStickyForm.CloseLoginWindow();
            //}
            ActiveSession.User = new Member();
            lblVersion.Text = string.Format("v {0}", Application.ProductVersion);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            objStickyForm.CloseLoginWindow();
        }

        private void SaveCredentials(Member obj)
        {
            //remember the username and password in configuration file            
            IssueTracker.Properties.Settings.Default.username = txtUserName.Text;
            IssueTracker.Properties.Settings.Default.password = txtPassword.Text;
            IssueTracker.Properties.Settings.Default.Save();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckEmpty())
            {
                Cursor.Current = Cursors.WaitCursor;
                Member obj = Service.Login.ValidateUserLogin(txtUserName.Text, txtPassword.Text);
                if (obj != null && obj.ID > 0)
                {
                    SaveCredentials(obj);
                    //objStickyForm.currentUser = obj;
                    ActiveSession.User = obj;
                    objStickyForm.ShowMainWindow();
                    //load statistics in sticky form
                    objStickyForm.LoadStatistics();
                    txtPassword.Text = string.Empty;
                    lblMessage.Text = string.Empty;
                    objStickyForm.CloseLoginWindow();
                }
                else
                {
                    lblMessage.Text = "Username or password did not match. Please try again.";
                    txtPassword.Text = string.Empty;
                    lblMessage.Visible = true;
                }
                Cursor.Current = Cursors.Default;
            }
        }

        public bool CheckEmpty()
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                errorProvider1.SetError(txtUserName, "Username field can not be empty.");
                return false;
            }
            else if (string.IsNullOrEmpty(txtPassword.Text))
            {
                errorProvider1.SetError(txtPassword, "Password field can not be empty");
                return false;
            }
            return true;
        }
    }
}
